//
//  YLContinuationDetailViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/14.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLContinuationDetailViewModel.h"
#import "YLGlobeHeader.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
#import "YLRegularFinancingDetailModel.h"
#import <YYModel.h>

@implementation YLContinuationDetailViewModel


- (NSArray<YLTitleContentModel *> *)models
{
    if (!_models) {
        _models = [NSArray array];
    }
    return _models;
}


- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [WHProgressHUD showStatus:@"正在加载续投详情..."];
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetOrderSingleInfo", YLURL];
                
                NSDictionary *param = @{@"ContractCode" : input};
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:param success:^(id responseObject) {
                    [WHProgressHUD dismiss];
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        YLRegularFinancingDetailModel *model = [YLRegularFinancingDetailModel yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        weakSelf.detailModel = model;
                        weakSelf.models = @[[YLTitleContentModel modelWithTitle:@"投资本金"
                                                                        content:[NSString stringWithFormat:@"%.2f", model.investmentAmount]
                                                                   contentColor:YLColor(50, 50, 50) hiddenLine:YES],
                                            
                                    [YLTitleContentModel modelWithTitle:@"追加金额"
                                                                content:[NSString stringWithFormat:@"%.2f", model.continueInvestmentAmount]
                                                           contentColor:YLColor(50, 50, 50) hiddenLine:YES],
                                            [YLTitleContentModel modelWithTitle:@"收益起始日" content:model.calulationDate
                                                           contentColor:YLColor(50, 50, 50) hiddenLine:YES],
                                    [YLTitleContentModel modelWithTitle:@"收益到期日" content:model.endTime
                                                           contentColor:YLColor(50, 50, 50) hiddenLine:YES],
                                    [YLTitleContentModel modelWithTitle:@"原合同编号" content:model.previousContractCode
                                                           contentColor:YLLOrgColor hiddenLine:YES]];
                        [subscriber sendNext:nil];
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [WHProgressHUD dismiss];
                    [subscriber sendCompleted];
                }];
                
                return nil;
                
            }];
        }];
    }
    return _requestCommand;
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}


@end
